<?php
class Model_News extends Model{

    protected static $_instance =  null;
	/**
     * @return Model_News
     */
	static public function getInstance(){
        if(is_null(self::$_instance))	
           self::$_instance = new self('news');     
        return self::$_instance;   
    }

 	/**
     * Get years list
     * @return array
     */
    public function getYears($published=false) {
        
        $sql = $this->_db->select()
                    ->distinct()
                    ->from($this->table() , array('year'=>'YEAR(news_date)'))
                    ->order('year DESC');
                    
        if($published)
             $sql->where('`published` = 1');           
        
        return $this->_db->fetchCol($sql);
    }  
    /**
     * (non-PHPdoc)
     * @see Model::queryAddFilters()
     */
    static public function queryAddFilters(Zend_Db_Select $sql, $filters)
    {
    	if(!is_array($filters) || empty($filters))
        	return;
        	
        $db = Application::getDbConnection();
        
        foreach ($filters as $k=>$v){
            if(empty($v))
               continue;
                
			if(is_array($v))
                $sql->where($db->quoteIdentifier($k) . ' IN(?)', $v);
            else
				if($k == 'year')
					$sql->where(' YEAR(`news_date`)=?', intval($v));
                else
                     $sql->where($db->quoteIdentifier($k) . ' =?', $v);
		}
    }
}